<template>
  <div :class="className" :style="{ height: '836px', width: '800px' }" />
</template>

<script>
import * as echarts from 'echarts';
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '150px'
    },
    chartData: {
      type: Array,
      default() {
        return []
      }
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      // immediate: true,
      handler(val) {
        console.log(val)
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.chartData.length && this.setOptions(this.chartData)
    },
    setOptions(value) {
      console.log(value)
      const radar = Object.keys(value[0].audienceFactorScore).map(item => {
        return {
          text: item,
          max: 5
        }
      })

      const data = value.map(item => {
        return {
          name: item.name,
          value: Object.values(item.audienceFactorScore)
        }
     })
     console.log(data, '----------------')

      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        legend: {
          bottom: '0px',
          right: '20px',
          textStyle: {
            color: '#fff'
          },
          data: value.map(item => item.name)
        },
        radar: [
          {
            indicator: radar
          },

        ],
        series: [
          {
            type: 'radar',
            tooltip: {
              trigger: 'item'
            },
            data: data
          },

        ]
      })
    }
  }
}
</script>
